package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.server.mapper.QuestionMapper;
import com.tanhua.server.pojo.Question;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @description:
 * @author: Administrator
 * @date: Created in 2021/2/28 10:31
 * @version: 0.0.1
 * @modified By:
 */
@Service
public class QuestionService {
    @Autowired
    private QuestionMapper questionMapper;

    public Question queryQuestion(Long userId){
        QueryWrapper<Question>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
       return this.questionMapper.selectOne(queryWrapper);
    }

    public Boolean saveQuestion(Long userId, String content) {
        //判断问题是否存在，如果存在进行修改，如果不存在进行新增
        try {
            QueryWrapper<Question>queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("user_id",userId);
            Question question = this.questionMapper.selectOne(queryWrapper);
            if(null==question){
                Question newQuestion=new Question();
                question.setUserId(userId);
                question.setTxt(content);
                this.questionMapper.insert(question);
            }else{
                question.setTxt(content);
                this.questionMapper.updateById(question);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
